package com.fenxiangle.yueding.feature.order.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.OrderBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.entity.bo.TeamBo;
import com.fenxiangle.yueding.entity.bo.UserImageBo;
import com.fenxiangle.yueding.feature.home.view.UserInfoActivity;
import com.fenxiangle.yueding.feature.mine.view.adapter.MyDemandMagageAdapter;
import com.fenxiangle.yueding.feature.mine.view.adapter.Share2Activity;
import com.fenxiangle.yueding.feature.order.contract.OrderContract;
import com.fenxiangle.yueding.feature.order.dependencies.order.DaggerOrderComponent;
import com.fenxiangle.yueding.feature.order.dependencies.order.OrderPresenterModule;
import com.fenxiangle.yueding.feature.order.view.adapter.ContactDetailAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @ProjectName: YueDing
 * @Package: com.fenxiangle.yueding.feature.order.view
 * @ClassName: ContactDetailActivity
 * @Description: 订单查看 联系列表
 * @Author:
 * @CreateDate: 2019/3/8 17:52
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/8 17:52
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@SuppressLint("Registered")
public class ContactDetailActivity extends BaseActivity implements OrderContract.View, OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_coupon)
    RecyclerView rvCoupon;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout smartRefesh;

    ContactDetailAdapter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<TeamBo> mData;
    private String userUid, orderId;
    @Inject
    OrderContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_contact_detail;
    }

    private String type;

    @Override
    protected void initView() {
        super.initView();
        initToolBar(toolbar, "订单详情", true, true);
        toolbar.findViewById(R.id.tv_comment_confirm).setVisibility(View.GONE);
        type = getIntent().getStringExtra("type");
        initAdapter();
    }


    public void initAdapter() {
        smartRefesh.setEnableLoadMore(false);
        smartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(rvCoupon);
        rvCoupon.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ContactDetailAdapter();
        mAdapter.setType(type);
        mAdapter.bindToRecyclerView(rvCoupon);
        mAdapter.setOnLoadMoreListener(this, rvCoupon);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void initInjector() {
        DaggerOrderComponent.builder().orderPresenterModule(new OrderPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        userUid = this.getIntent().getStringExtra("user_id");
        orderId = this.getIntent().getStringExtra("orderId");
        getData();
    }

    private void getData() {
        pageIndex = 1;
        mPresenter.getUserList(pageIndex, pageSize, userUid, orderId, 1);
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.getUserList(pageIndex, pageSize, userUid, orderId, 1);
    }

    @Override
    public void toComments(String s) {

    }

    @Override
    public void confirmOrder(String s) {
        showMsg("确定订单成功");
        getData();
    }

    @Override
    public void showUserListSuccess(List<TeamBo> workBoPageData) {
        smartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showSheetSuccess(UserImageBo workBoPageData) {

    }

    @Override
    public void showcancleSheetSuccess(UserImageBo workBoPageData) {

    }

    @Override
    public void showPublishListSuccess(List<PublishDemandBo> workBoPageData) {

    }

    @Override
    public void showSheetListSuccess(List<PublishDemandBo> workBoPageData) {

    }

    @Override
    public void showPublishListError(String error) {
        showMsg(error);
    }

    @Override
    public void showPublishListMoreSuccess(List<PublishDemandBo> msg) {

    }

    @Override
    public void showPublishListMoreError(String msg) {

    }

    @Override
    public void showPublishListEmpty() {
        smartRefesh.finishLoadMore(true);
        smartRefesh.finishRefresh(true);
    }

    @Override
    public void showAcceptSuccess(String s) {

    }

    @Override
    public void showInsufficientBalance() {

    }

    @Override
    public void showRefuseSuccess(String s) {

    }

    @Override
    public void showError(String error) {
        smartRefesh.finishRefresh();
        smartRefesh.finishLoadMore();
    }

    @Override
    public void showConfrimTxSuccess() {

    }

    @Override
    public void showAppealTxSuccess(String s) {
        showMsg("维权成功");
        getData();
    }

    @Override
    public void showCancleSuccess() {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mAdapter.setEnableLoadMore(false);
        initData();
    }

    RxPermissions rxPermissions;
    ClipboardManager myClipboard;
    ClipData myClip;

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.btn_my_activist://维权
                mPresenter.appeal(new OrderBo(orderId, mData.get(position).getUserUid(), 0));
                break;
            case R.id.btn_my_publish_confrim://确定订单
                if (mData.get(position).getGrabStatus() == 6) {//维权中
                    Intent intent = new Intent(this, Share2Activity.class);
                    startActivity(intent);
                } else {//维权
                    mPresenter.confirmOrder(new OrderBo(orderId, mData.get(position).getUserUid(), ""));
                }

                break;
            case R.id.btn_call_phone:

                rxPermissions = new RxPermissions(this);
                rxPermissions
                        .request(Manifest.permission.CALL_PHONE)
                        .subscribe(granted -> {
                            if (granted) { // Always true pre-M
                                diallPhone(mData.get(position).getUserPhone());
                            } else {
                                showMsg("请先开启拨打电话权限!");
                            }
                        });
                break;
            case R.id.btn_copy_wx:
                if (TextUtils.isEmpty(mData.get(position).getWeChat())) {
                    T.showShort("该用户未设置微信号！");
                } else {
                    myClipboard = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
                    myClip = ClipData.newPlainText("text", mData.get(position).getWeChat());
                    myClipboard.setPrimaryClip(myClip);
                    T.showShort("文本已复制！");
                }
                break;
            case R.id.iv_level_head:
                Intent intent = new Intent(this, UserInfoActivity.class);
                intent.putExtra("id", mData.get(position).getUserUid());
                startActivity(intent);
                break;


        }
    }

    public void diallPhone(String phoneNum) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        //url:统一资源定位符
        //uri:统一资源标示符（更广）
        intent.setData(Uri.parse("tel:" + phoneNum));
        //开启系统拨号器
        startActivity(intent);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
